<?php

declare(strict_types=1);

namespace app\admin\model;

use think\Model;

class CustomerContact extends Model
{
    // 定义数据表名
    protected $name = 'customer_contact';

    // 定义主键
    protected $primaryKey = 'id';

    // 定义创建时间字段名
    protected $createTime = 'create_time';

    // 定义更新时间字段名
    protected $updateTime = 'update_time';
    // 定义自动时间戳
    protected $autoWriteTimestamp = true;

    // 设置字段信息
    protected $schema = [
        'id'          => 'int',
        'cusid'       => 'int',
        'cname'       => 'string',
        'gender'      => 'int',
        'birthday'    => 'date',
        'phone'       => 'string',
        'email'       => 'string',
        'qq'          => 'string',
        'telegram'    => 'string',
        'wechat'      => 'string',
        'whatsapp'    => 'string',
        'dingtalk'    => 'string',
        'country'     => 'string',
        'address'     => 'string',
        'department'  => 'string',
        'position'    => 'string',
        'level'       => 'int',
        'decision'    => 'int',
        'tags'        => 'string',
        'remark'      => 'string',
        'create_time' => 'datetime',
        'creator'     => 'int',
        'update_time' => 'datetime',
        'updater'     => 'int',
        'is_del'      => 'int',
    ];



    // 定义一个通用的访问器来获取属性值
    protected $attributes = [
        'gender' => [
            1 => '男',
            2 => '女'
        ],
        'level' => [
            1 => '高层',
            2 => '中层',
            3 => '基层',
        ],

        'decision' => [
            1 => '关键决策人',
            2 => '分项决策人',
            3 => '商务决策人',
            4 => '技术决策人',
            5 => '财务决策人',
            6 => '使用人',
            7 => '意见影响人',
            8 => '普通人',
            99 => '其他',
        ]
    ];

    public function getAttr($name)
    {
        // 检查是否是要处理的特殊属性
        if (strpos($name, '_value') !== false) {
            $originalField = str_replace('_value', '', $name);
            if (isset($this->attributes[$originalField])) {
                $value = $this->getData($originalField);
                return $this->attributes[$originalField][$value] ?? '未知';
            }
        }

        // 如果不是特殊属性，调用父类的方法
        return parent::getAttr($name);
    }


    public function getFieldEnum($field)
    {

        if ('ALL' === strtoupper($field)) {
            return [
                'gender' => $this->attributes['gender'],
                'level' => $this->attributes['level'],
                'decision' => $this->attributes['decision'],
            ];
        } else {
            return [$field => $this->attributes[$field]];
        }
    }



    // 定义关联方法
    public function creator()
    {
        // 假设'creator'是外键字段，'id'是Customer模型的主键
        return $this->belongsTo('Operator', 'creator', 'id');
    }

    public function updater()
    {
        // 假设'updater'是外键字段，'id'是Customer模型的主键
        return $this->belongsTo('Operator', 'updater', 'id');
    }

    // 定义获取器
    public function getCreatorValueAttr($value, $data)
    {
        return $this->getRelatedCustomerName('creator');
    }

    public function getUpdaterValueAttr($value, $data)
    {
        return $this->getRelatedCustomerName('updater');
    }

    // 私有辅助方法
    private function getRelatedCustomerName($relationName)
    {
        $relation = $this->getRelation($relationName);

        if (!$relation) {
            $relation = $this->$relationName()->find();
        }

        return $relation ? $relation->cusname : null;
    }


    public function getCusidValueAttr($value, $data)
    {

        if (is_null($data['cusid']) || '' == $data['cusid']) {
            return [];
        }
        $cus = Customer::where(['id' => $data['cusid']])->field('cusname')->select()->toArray();
        return implode(',', array_column($cus, 'cusname'));
    }
}
